
const tokenPrefix = 'alpha_token';
// 发起请求 - 默认post
const app = getApp();
// 请求方法
const request_api = function (url, req_data = {}, is_show_loading = true, req_method = 'POST', header_data = {}) {
    var __this = this;
    // 显示加载条
    if (is_show_loading) {
        wx.showLoading ({
            title: '加载中',
            mask: true
        })
    }
    // console.log("tokenPrefix:",wx.getStorageSync (tokenPrefix))
    header_data = Object.assign ({}, {
        'Content-Type': 'application/json',
        'Authorization': "Bearer " + wx.getStorageSync (tokenPrefix),
        "client-name": "wechat",
        "version-name": "1.2"
    }, header_data);
    // 发起请求
    return new Promise ((resolve, reject) => {
        // 发起网络请求
        wx.request ({
            url: app.globalData.alphaBaseUrl + url,
            method: req_method,
            header: header_data,
            data: req_data,
            success (res2) {
                if (res2.statusCode == 200) {
                    if (res2.data.code == 200) {
                        resolve (res2.data)
                    } else if (res2.data.code == 0) {
                        setTimeout (function () {
                            wx.showToast ({
                                title: res2.data.msg,
                                icon: 'none',
                                duration: 2000,
                            });
                        }, 200)
                        reject (res2.data)
                    } else if (res2.data.code == 401) {
                        // token无效
                        wx.removeStorage ({
                            key: tokenPrefix
                        });
                        setTimeout (function () {
                            wx.showToast ({
                                title: res2.data.msg,
                                icon: 'none',
                                duration: 2000,
                            });
                        }, 200)
                        wx.navigateTo ({
                            url: '/alpha/pages/alpha_login/index',
                        });
                    } else if (res2.data.code == 8000100) {
                        // 用户不存在
                        setTimeout (function () {
                            wx.showToast ({
                                title: '请尝试手机号登录',
                                icon: 'none',
                                duration: 2000,
                            });
                        }, 200)
                    } else if (res2.data.code == 100009 || res2.data.code == 8000102) {
                        // 用户不存在
                        setTimeout (function () {
                            wx.showToast ({
                                title: '请联系IT后台创建账号',
                                icon: 'none',
                                duration: 2000,
                            });
                        }, 200)
                    } else {
                        setTimeout (function () {
                            wx.showToast ({
                                title: res2.data.msg,
                                icon: 'none',
                                duration: 2000,
                            });
                        }, 200)
                        reject (res2.data);
                    }
                } else if (res2.statusCode == 500) {
                    setTimeout (function () {
                        wx.showToast ({
                            title: '系统错误',
                            icon: 'none',
                            duration: 2000,
                        });
                    }, 200)
                }
            },
            fail: (err) => {
                // 请求失败返回的消息
                reject (err)
            },
            complete () {
                // 请求完做的事
                if (is_show_loading) {
                    wx.hideLoading ();
                }
            }
        })
    })
}

// 2025-01-20 临时-本地开发调试-会删除掉
const request_api_test = function (url, req_data = {}, is_show_loading = true, req_method = 'POST', header_data = {}) {
    var __this = this;
    // 显示加载条
    if (is_show_loading) {
        wx.showLoading ({
            title: '加载中',
            mask: true
        })
    }
    // console.log("tokenPrefix:",wx.getStorageSync (tokenPrefix))
    header_data = Object.assign ({}, {
        'Content-Type': 'application/json',
        'Authorization': "Bearer " + wx.getStorageSync (tokenPrefix),
        "client-name": "wechat",
        "version-name": "1.2"
    }, header_data);
    // 发起请求
    return new Promise ((resolve, reject) => {
        // 发起网络请求
        wx.request ({
            url: url,
            method: req_method,
            header: header_data,
            data: req_data,
            success (res2) {
                if (res2.statusCode == 200) {
                    if (res2.data.code == 200) {
                        resolve (res2.data)
                    } else if (res2.data.code == 0) {
                        setTimeout (function () {
                            wx.showToast ({
                                title: res2.data.msg,
                                icon: 'none',
                                duration: 2000,
                            });
                        }, 200)
                        reject (res2.data)
                    } else if (res2.data.code == 401) {
                        // token无效
                        wx.removeStorage ({
                            key: tokenPrefix
                        });
                        setTimeout (function () {
                            wx.showToast ({
                                title: res2.data.msg,
                                icon: 'none',
                                duration: 2000,
                            });
                        }, 200)
                        wx.navigateTo ({
                            url: '/alpha/pages/alpha_login/index',
                        });
                    } else if (res2.data.code == 8000100) {
                        // 用户不存在
                        setTimeout (function () {
                            wx.showToast ({
                                title: '请尝试手机号登录',
                                icon: 'none',
                                duration: 2000,
                            });
                        }, 200)
                    } else if (res2.data.code == 100009 || res2.data.code == 8000102) {
                        // 用户不存在
                        setTimeout (function () {
                            wx.showToast ({
                                title: '请联系IT后台创建账号',
                                icon: 'none',
                                duration: 2000,
                            });
                        }, 200)
                    } else {
                        setTimeout (function () {
                            wx.showToast ({
                                title: res2.data.msg,
                                icon: 'none',
                                duration: 2000,
                            });
                        }, 200)
                        reject (res2.data);
                    }
                } else if (res2.statusCode == 500) {
                    setTimeout (function () {
                        wx.showToast ({
                            title: '系统错误',
                            icon: 'none',
                            duration: 2000,
                        });
                    }, 200)
                }
            },
            fail: (err) => {
                // 请求失败返回的消息
                reject (err)
            },
            complete () {
                // 请求完做的事
                if (is_show_loading) {
                    wx.hideLoading ();
                }
            }
        })
    })
}

//生成年份字符串数组
const generateYearArray = function (callback) {
    var currentYear = new Date().getFullYear();
    var pastYears = 7;
    var futureYears = 3;
    var yearArray = [];
    // 生成过去7年的年份
    for (var i = currentYear - pastYears; i <= currentYear; i++) {
        yearArray.push(i);
    }
    // 生成未来3年的年份
    for (var j = currentYear + 1; j <= currentYear + futureYears; j++) {
        yearArray.push(j);
    }

    if( typeof callback === 'function'){
        callback(yearArray,pastYears);
    }
}

//返回树结构
const buildTree = function (data) {
    const tree = [];
    const hashTable = Object.create (null);
    // 1. 创建哈希表并初始化每个节点
    data.forEach (item => {
        hashTable[item.id] = {...item, children: []};
    });

    // 2. 构建树结构
    data.forEach (item => {
        hashTable[item.id].pp_id = 0
        if (item.parentId == 0) {
            // 如果是根节点，直接放入树的顶层
            tree.push (hashTable[item.id]);
        } else {
            // 否则找到父节点，放入其 children 数组
            const parent = hashTable[item.parentId];
            hashTable[item.id].pp_id = hashTable[item.parentId]!==undefined?hashTable[item.parentId].parentId: 0;
            if (parent) {
                parent.children.push (hashTable[item.id]);
            }
        }
    });
    return tree;
}

module.exports = {
    tokenPrefix,
    request_api,
    request_api_test,
    buildTree,
    generateYearArray
};